package com.ql.mangersystem.service.impl;

import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.dto.NoticeAddDTO;
import com.ql.mangersystem.entity.dto.NoticePgeDTO;
import com.ql.mangersystem.entity.dto.NoticeUpdateDTO;
import com.ql.mangersystem.entity.dto.PageDTO;
import com.ql.mangersystem.entity.po.Notice;
import com.ql.mangersystem.mapper.NoticeMapper;
import com.ql.mangersystem.mapper.UserMapper;
import com.ql.mangersystem.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public int save(NoticeAddDTO noticeAddDTO) {
//        System.out.println(noticeAddDTO);
        String time = noticeAddDTO.getTime();
        Integer userid = noticeAddDTO.getUserid();
        String title = noticeAddDTO.getTitle();
        String content = noticeAddDTO.getContent();
        return noticeMapper.save(userid,time,title,content);
    }

    @Override
    public int update(NoticeUpdateDTO noticeUpdateDTO) {
        Integer id = noticeUpdateDTO.getId();
        String title = noticeUpdateDTO.getTitle();
        String content = noticeUpdateDTO.getContent();
//        Integer open = noticeUpdateDTO.getOpen();
        return noticeMapper.update(id,title,content);
    }

    @Override
    public int delete(Integer id) {
        return noticeMapper.delete(id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return noticeMapper.deleteBatch(ids);
    }

    @Override
    public List<Notice> selectAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public Notice getById(Integer id) {
        return noticeMapper.getById(id);
    }

    @Override
    public Page<Notice> page(NoticePgeDTO noticePgeDTO) {
//        System.out.println(noticePgeDTO);
        Integer pageNum = noticePgeDTO.getPageNum();
        String title = noticePgeDTO.getTitle();
        Integer pageSize = noticePgeDTO.getPageSize();
        Integer skipNum = (pageNum - 1) * pageSize;
        List<Notice> list = noticeMapper.pageList(skipNum,pageSize,title);
        Integer total = noticeMapper.count(title);
//        String username = userMapper.selectName();
//        System.out.println(list);
        Page<Notice> page = new Page<>();
        page.setTotal(total);
        page.setList(list);
        return page;
    }

    @Override
    public Page<Notice> pageAll(PageDTO pageDTO) {
        Integer pageNum = pageDTO.getPageNum();
        Integer pageSize = pageDTO.getPageSize();
        Integer skipNum = (pageNum - 1) * pageSize;
        List<Notice> list = noticeMapper.page(skipNum,pageSize);
        Integer total = noticeMapper.countNum();
        Page<Notice> page = new Page<>();
        page.setTotal(total);
//        System.out.println(list);
        page.setList(list);
        return page;
    }

    @Override
    public int changeOpen(Integer open,Integer id) {
        return noticeMapper.openChange(open,id);
    }
}
